package cn.org.timespace.easyframe.action.permission;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.org.timespace.easyframe.action.EFBaseAction;
import cn.org.timespace.easyframe.model.permission.EfUser;
import cn.org.timespace.easyframe.model.permission.EfUserRole;
import cn.org.timespace.easyframe.service.permission.EfUserRoleService;
import cn.org.timespace.easyframe.service.permission.EfUserService;

/**
 * <p>
 * 用户角色Action
 * </p>
 * 
 * @version 1.0, 11/02/26
 * @since 1.0
 * @author wubo
 */

public class EfUserRoleAction extends EFBaseAction{

	private static final long serialVersionUID = -3416183313291298070L;

	private static Log log=LogFactory.getLog(EfUserRoleAction.class);
	
	private EfUserService efUserService;
	private EfUserRoleService efUserRoleService;
	
	public EfUserRoleAction()
	{
		super(new EFUserModel());
	}
	
	EFUserModel efUserModel=(EFUserModel)this.getModel();
	
	public String doList() throws Exception{
		
		//查询所有的用户数据
		List<EfUser> efUserList = 
			efUserService.findByCondition(efUserModel);
		getRequest().setAttribute(VOLIST, efUserList);
		
		//查询该角色目前已经关联的用户
		String roleId = getRequest().getParameter("roleId");
		List<EfUserRole> efUserRoleList = efUserRoleService.findByCondition(roleId);
		getRequest().setAttribute("efUserRoleList", efUserRoleList);
		getRequest().setAttribute("efUserRoleListSize", efUserRoleList.size());
		
		//将roleId传递至页面
		getRequest().setAttribute("roleId", roleId);
		
		return LIST;
	}
	
	public String doSave() throws Exception{
		String pageMsg = "保存成功";
		
		//获取页面传过来的用户ID信息
		String roleId = getRequest().getParameter("roleId");
		String userIds = getRequest().getParameter("userIds");
		if(log.isInfoEnabled()) log.info("角色授权[角色："+roleId+"   选择的用户："+userIds+"]");
		
		try{
			efUserRoleService.saveUserRole(roleId, userIds);
		}catch (Exception e){
			pageMsg = "保存失败！原因是："+e.getMessage();
		}
		
		getRequest().setAttribute(PAGEMSG,pageMsg);

		return doList();
	}

	public EfUserService getEfUserService() {
		return efUserService;
	}
	public void setEfUserService(EfUserService efUserService) {
		this.efUserService = efUserService;
	}
	
	public EfUserRoleService getEfUserRoleService() {
		return efUserRoleService;
	}
	public void setEfUserRoleService(EfUserRoleService efUserRoleService) {
		this.efUserRoleService = efUserRoleService;
	}

}
